#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# @Time    : 2024/9/29 15:59 
# @Author  : SumKids 
# @Site    :  
# @File    : 05_window_show.py
# @Software: PyCharm
# @Description:

from PySide6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit, QVBoxLayout
from PySide6.QtGui import QAction
from PySide6.QtCore import Qt
import sys


'''
        窗口上下文   ---     右键菜单
'''


class MyWidget(QWidget):

    def __init__(self) -> None:
        super().__init__()
        self.setWindowTitle('07_ContextMenuPolicy_上下文_右键菜单')
        self.resize(400, 200)

        # =====================================  【窗体】的上下文 ==================================================
        # DOC 01: 设置窗体的上下文策略
        self.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu)

        # DOC 02: 创建Action，并添加到上下文中
        self.action1 = QAction('打开文件')
        self.action2 = QAction('关闭文件')

        self.addActions([self.action1, self.action2])

        # DOC 03: 给Action添加触发事件
        self.action1.triggered.connect(lambda : print('打开文件'))
        self.action2.triggered.connect(lambda : print('关闭文件'))

        # =====================================  【控件】的上下文 ==================================================

        self.lineEdit = QLineEdit('hhhh')
        self.lb = QLabel('_____')

        self.lineEdit.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu)
        self.action21 = QAction('发送值')
        self.action22 = QAction('清空值')
        self.lineEdit.addActions([self.action21, self.action22])

        self.action21.triggered.connect(lambda: self.lb.setText(self.lineEdit.text()))
        self.action22.triggered.connect(lambda: self.lineEdit.clear())

        # 布局
        layout = QVBoxLayout()
        layout.addWidget(self.lineEdit)
        layout.addWidget(self.lb)
        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWidget()
    window.show()
    app.exec()
